﻿using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.ConstrainedExecution;
using System.Text;
using UnityEngine;

public class TestCMD : MonoBehaviour
{
    static string protectPath
    {
        get
        {
            //return Path.Combine(Application.dataPath, "../../");
            string dataPath = Application.dataPath.Replace("\\", "/");
            string path = dataPath.Replace("ChatAppUnity/Assets", "");
            if (path[path.Length - 1] == '/')
                path = path.Substring(0, path.Length - 1);
            return path;
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }
    [ContextMenu("TestCMDProto")]
    public void TestCMDProto()
    {
        StringBuilder cmdText = new StringBuilder();
        string pan = protectPath.Substring(0, 2);

        string progenGen = $"{protectPath.Replace("/","\\")}\\ProtoGen";
        //cmdText.Append($"{pan}\n");
        cmdText.Append($"cd {progenGen}\n");// cmdText.Append($"cd {protectPath.Substring(2)}\\ProtoGen\n");
                                                       //cmdText.Append("protogen.exe -i:.\\FixedVec2.proto -o:.\\FixedVec2.cs");

        Encoding utf8 = Encoding.UTF8;
        //string fileName = GetFileNameWithPath(proto, false);
        cmdText.Append("protoc.exe .\\Proto\\Logic\\AccountRequestData_Data.proto --csharp_out=:.\\Export\\");
        
       // cmdText.Append(string.Format("protoc.exe .\\{0}\\{01}.proto --csharp_out=:.\\Export\\\n", protoDir, fileName, fileName + "_Auto"));

#if LOG
        GameDebug.Log($"cmdText :{cmdText.ToString()}");
#endif
        var outputLog = RunCmd(cmdText.ToString());

        GameDebug.Log($"outputLog :{outputLog}");
    }
    static string RunCmd(string cmd)
    {
        Process proc = new Process();
        proc.StartInfo.CreateNoWindow = false;
        proc.StartInfo.FileName = "cmd.exe";
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardError = true;
        proc.StartInfo.RedirectStandardInput = true;
        proc.StartInfo.RedirectStandardOutput = true;
        //proc.StartInfo.Arguments = cmd+"\nexit";// "/c " + command;
        GameDebug.Log("执行命令:\n" + cmd);
        proc.Start();

        proc.StandardInput.WriteLine(cmd);
        proc.StandardInput.WriteLine("exit");

        StreamReader reader = proc.StandardOutput;
        string output = "";

        while (!reader.EndOfStream)
        {
            string cur = reader.ReadLine()+"\n";
            //GameDebug.Log("out str:" + cur+"\n");
            output += cur;

        }
        proc.Close();
        return output;
    }

    [ContextMenu("TestCMDProtoVersion")]
    public void TestCMDProtoVersion()
    {
        // 要执行的命令
        string command = "C:\\Develop\\protoc-25.2-win64\\bin\\protoc.exe --version";

        // 创建一个新的进程对象
        Process process = new Process();

        // 配置进程启动信息
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = "cmd.exe"; // 指定要执行的命令行解释器
        startInfo.Arguments = "/c " + command; // 指定要执行的命令

        // 重定向标准输出，以便在Unity中获取输出结果
        startInfo.RedirectStandardOutput = true;
        startInfo.UseShellExecute = false;
        startInfo.CreateNoWindow = true;
        startInfo.RedirectStandardError = true;
        startInfo.RedirectStandardInput = true;
        startInfo.RedirectStandardOutput = true;

        process.StartInfo = startInfo;

        // 启动进程
        process.Start();
        //process.StandardInput.WriteLine(command);
        //process.StandardInput.WriteLine("exit");

        string output = "";

        while (!process.StandardOutput.EndOfStream)
            output += process.StandardOutput.ReadLine() + "\n";

        UnityEngine.Debug.Log("output:" + output );

        // 等待进程退出
        process.WaitForExit();

        // 关闭进程
        process.Close();

        //        StringBuilder cmdText = new StringBuilder();
        //        cmdText.Append("protoc.exe --version");

        //#if LOG
        //        GameDebug.Log($"cmdText :{cmdText.ToString()}");
        //#endif
        //        var outputLog = RunCmd(cmdText.ToString());

        //        GameDebug.Log($"outputLog :{outputLog}");
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}
